/**
 * Created whit IntelliJ IDEA.
 * Description：
 * User：ZHONGCHEN
 * Date:2022-02-07
 * Time:11:52
 */
public class TestDemo {
    int ans = 0;
    int[][] g;
    int m;
    int k;

    public  int getMaximumGold(int[][] grid) {
        g = grid;
        m = g.length;
        k = g[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < k; j++) {
                if (g[i][j] != 0) {
                    getGold(i, j, 0);
                }
            }
        }
        return ans;
    }

    public  void getGold(int i, int j, int gold) {
        if (i == -1 || i == m || j == -1 || j == k || g[i][j] == 0) {
            return;
        }
        int golds = g[i][j];
        gold += golds;
        g[i][j] = 0;
        ans = Math.max(ans, gold);
        getGold(i + 1, j, gold);
        getGold(i - 1, j, gold);
        getGold(i, j + 1, gold);
        getGold(i, j - 1, gold);
        g[i][j] = golds;
    }

    public static void main1(String[] args) {
        int[][] g = {{34, 0, 1, 0, 0, 0}, {0, 0, 2, 0, 1, 0}, {5, 4, 3, 7, 4, 2}, {0, 0, 5, 0, 1, 4}, {0, 0, 5, 0, 2, 3}};
        TestDemo testDemo = new TestDemo();
        System.out.println(testDemo.getMaximumGold(g));
    }
    public static void main(String[] args) {
        int[][] g = {
                {34, 0, 1, 0, 0, 0},
                {0, 0, 2, 0, 1, 0},
                {5, 4, 3, 7, 4, 2},
                {0, 0, 5, 0, 1, 4},
                {0, 0, 5, 0, 2, 3}
        };
        test s = new test();
        System.out.println(s.getMaximumGold(g));
    }
}
